package newtank.view;

import entity.Direction;
import entity.MoveGenerator;
import entity.Position;
import entity.Tank;
import newtank.view.context.BulletViewContext;
import newtank.view.context.TankViewContext;
import tool.GlobalUniqueGenerator;

import java.awt.*;

/**
 * @author lijiahao
 * @since 2022/1/27
 */
public class TankView implements PaintView {

    private final TankViewContext tankViewContext;

    private final String unique;

    public TankView(TankViewContext tankViewContext) {
        this.tankViewContext = tankViewContext;
        this.unique = tankViewContext.getTankId();
    }

    @Override
    public void draw(Graphics2D g2d) {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.PINK);
        // 1. 绘制坦克的身体
        drawTankBody(tankViewContext.getPosition(), g2d);
        // 2.绘制坦克的炮口
        drawTankGun(tankViewContext.getPosition(), tankViewContext.getDirection(), g2d);
    }

    @Override
    public String getIdentity() {
        return unique;
    }

    private void drawTankBody(Position position, Graphics2D g2d) {
        g2d.fillArc(position.getX(), position.getY(), Tank.TWidth, Tank.THeight, 0, 360);
    }

    private void drawTankGun(Position position, Direction dir, Graphics2D g2d) {
        int gunX = 0;
        int gunY = 0;
        int destX = 0;
        int destY = 0;
        float[] dash = new float[] { Tank.GunLength };
        BasicStroke bs2 = new BasicStroke(
                4,
                BasicStroke.CAP_SQUARE,
                BasicStroke.JOIN_MITER,
                10.0f,
                dash,
                0.0f
        );
        g2d.setStroke(bs2);
        g2d.setColor(Color.WHITE);
        switch (dir) {
            case LEFT:
                gunX = position.getX()+Tank.TWidth/2+2;
                gunY = position.getY()+Tank.THeight/2;
                destX = gunX-tankViewContext.getGunLength();
                destY = gunY;
                tankViewContext.setGunPosition(new Position(destX, destY-1));
                break;
            case RIGHT:
                gunX = position.getX()+Tank.TWidth/2-2;
                gunY = position.getY()+Tank.THeight/2;
                destX = gunX+tankViewContext.getGunLength();
                destY = gunY;
                tankViewContext.setGunPosition(new Position(destX, destY-1));
                break;
            case UP:
                gunX = position.getX()+Tank.TWidth/2;
                gunY = position.getY()+Tank.THeight/2+2;
                destX = gunX;
                destY = gunY-tankViewContext.getGunLength();
                tankViewContext.setGunPosition(new Position(destX-1, destY));
                break;
            case DOWN:
                gunX = position.getX()+Tank.TWidth/2;
                gunY = position.getY()+Tank.THeight/2-2;
                destX = gunX;
                destY = gunY+tankViewContext.getGunLength();
                tankViewContext.setGunPosition(new Position(destX-1, destY));
                break;
            default:
                break;
        }
        g2d.drawLine(gunX, gunY, destX, destY);
    }

    @Override
    public void move() {
        MoveGenerator.move(tankViewContext.getPosition(), tankViewContext.getDirection(), tankViewContext.getSpeed(), this.getClass());
    }

    @Override
    public void turnLeft() {
        tankViewContext.setDirection(Direction.LEFT);
    }

    @Override
    public void turnRight() {
        tankViewContext.setDirection(Direction.RIGHT);
    }

    @Override
    public void turnDown() {
        tankViewContext.setDirection(Direction.DOWN);
    }

    @Override
    public void turnUp() {tankViewContext.setDirection(Direction.UP); }

    public void shooting(BulletViewContext bulletViewContext) {
        bulletViewContext.setAlive(Boolean.TRUE);
        bulletViewContext.setUnique(this.getIdentity() + ":bullet:"+ GlobalUniqueGenerator.getGlobalId());
        bulletViewContext.setTankViewContext(this.tankViewContext);
        bulletViewContext.setDirection(tankViewContext.getDirection());
        bulletViewContext.setPosition(tankViewContext.getGunPosition());
        bulletViewContext.setSpeed(5);
    }
}
